package com.example.validatestudy.demos.tread.juc;

import java.util.concurrent.*;

public class FutureTest {
    /**
     * 练习Future类
     * V get() 阻塞获取结果
     * V get(long timeout, TimeUnit unit) 阻塞获取结果，过期不候
     * boolean isDone() 判断执行完成状态
     */
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        FutureTask<Integer> future = new FutureTask<Integer>(()->{
            System.out.println("执行callable");
            Thread.sleep(3000);
            return 1;
        });
        new Thread(future).start();
        try {
            System.out.println(future.get(1000, TimeUnit.MILLISECONDS));
        } catch (TimeoutException e) {
            System.out.println("执行callable超时");
        }
        //isDone()不阻塞
        if(future.isDone()){
            System.out.println("callable执行完成");
        }
        //get()阻塞，无次数限制
        System.out.println("第一次阻塞获取："+future.get());
        System.out.println("第二次阻塞获取："+future.get());
    }
}
